package com.first.agent.dao.impl;

import com.first.agent.dao.CustomerDao;
import com.first.agent.dbutils.JdbcBuild;
import com.first.agent.pojo.AgentCustomer;
import com.first.agent.pojo.Contacts;
import com.first.agent.pojo.Keyword;

import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lxm
 * Date: 2022/11/15 10:12
 * Description:
 * Version: V1.0
 */
public class CustomerDaoImp extends JdbcBuild implements CustomerDao {
    @Override
    public List customerQuery(int page, int limit,String customerName) throws SQLException {
        List<AgentCustomer> list;

        if (customerName!=null&&!"".equals(customerName)) {
            String sql = "select * from agent_customer where company_name like ? limit ?,?";
            Object[] par = {'%'+customerName+'%', page, limit};
           list = super.queryListEntity(AgentCustomer.class, sql, par);
        }else{
            String sql = "select * from agent_customer limit ?,?";
            Object[] par = {page, limit};
           list = super.queryListEntity(AgentCustomer.class, sql, par);
        }



        //将联系人存入
//        (select cus_id from agent_customer where company_name=?)
        String sql1="select * from contacts where con_agent_id=?";
        for (AgentCustomer customer : list) {
            Object[] par2={customer.getCus_id()};
            Contacts contact = super.queryEntity(Contacts.class, sql1, par2);
            if (contact!=null){
                customer.setCon_name(contact.getCon_name());
                customer.setCon_agent_id(customer.getCus_id());
                customer.setCon_email(contact.getCon_email());
                customer.setCon_tel(contact.getCon_tel());
                customer.setCon_fax(contact.getCon_fax());
                customer.setCon_duties(contact.getCon_duties());
                customer.setContacts(contact);
            }

            /*if(contact.getCon_name()!=null&&!"".equals(contact.getCon_name())) {
                customer.setCon_name(contact.getCon_name());
                customer.setCon_agent_id(customer.getCus_id());
                customer.setCon_email(contact.getCon_email());
                customer.setCon_tel(contact.getCon_tel());
                customer.setCon_fax(contact.getCon_fax());
                customer.setCon_duties(contact.getCon_duties());
                customer.setContacts(contact);
            }*/
        }
        return list;
    }

    @Override
    public int countCustomer(String customerName) throws SQLException {
        if (customerName!=null&&!"".equals(customerName)){
            String sql="select count(*) from agent_customer where company_name=?";
            Object[] par={customerName};
            return super.findAllForInt(sql, par);
        }else {
            String sql="select count(*) from agent_customer";

        return super.findAllForInt(sql);
        }
    }
    //添加
    @Override
    public int addCustomer(AgentCustomer customer, Contacts contacts) throws SQLException {

        String sql="INSERT INTO agent_customer(company_name, company_home," +
                "company_type,state,represent,registration_time,document_type, " +
                "document_num, country,provinces,city,area,company_fax,company_tel,company_address,note) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] par={customer.getCompany_name(),customer.getCompany_home(),customer.getCompany_type(),customer.getState(),customer.getRepresent(),customer.getRegistration_time(),customer.getDocument_type(),customer.getDocument_num(),customer.getCountry(),customer.getProvinces(),customer.getCity(),customer.getArea(),customer.getCompany_fax()
                ,customer.getCompany_tel(),customer.getCompany_address(),customer.getNote()};
        int i0 = super.executeUpdate(sql, par);

        String sql2="insert into contacts(con_name,con_tel,con_fax,con_email,con_duties,con_agent_id) values(?,?,?,?,?,(select cus_id from agent_customer where company_name=?))";
        Object[] par2={contacts.getCon_name(),contacts.getCon_tel(), contacts.getCon_fax(), contacts.getCon_email(), contacts.getCon_duties(),customer.getCompany_name()};
        int i1 = super.executeUpdate(sql2, par2);
        if(i0>0&&i1>0){
            return i0;
        }else{
        return -1;
    }}
    //停用
    @Override
    public int delCustomer(Integer cus_id) throws SQLException {
        String sql="update agent_customer set state=2 where cus_id=?";
        Object[] par={cus_id};
        return super.executeUpdate(sql,par);
    }
    //启用
    @Override
    public int openCustomer(Integer cus_id) throws SQLException {
        String sql="update agent_customer set state=1 where cus_id=?";
        Object[] par={cus_id};
        return super.executeUpdate(sql,par);
    }

    @Override
    public int updateCustomer(AgentCustomer customer, Contacts contacts) throws SQLException {
        String sql= "update agent_customer set company_name=?, company_home=?, company_type=?, " +
                "state=?, represent=?, registration_time=?, document_type=?, document_num=?, " +
                "country=?, provinces=?, city=?, area=?, company_fax=?, company_tel=?, " +
                "company_address=?, note=? where cus_id=?";
        Object[] par={customer.getCompany_name(),customer.getCompany_home(),customer.getCompany_type(),customer.getState(),
                customer.getRepresent(),customer.getRegistration_time(),customer.getDocument_type(),customer.getDocument_num(),
                customer.getCountry(),customer.getProvinces(),customer.getCity(),customer.getArea(),customer.getCompany_fax()
                ,customer.getCompany_tel(),customer.getCompany_address(),customer.getNote(),customer.getCus_id()};
        int i0 = super.executeUpdate(sql, par);
        int i1=0;
        System.out.println(contacts.getCon_id());

        //根据之前是否有联系人进行新增或更新
        String sql3="select con_id from contacts where con_agent_id=?";
        Object[] par3={customer.getCus_id()};
        int allForInt = super.findAllForInt(sql3, par3);
        if(allForInt>0){
            String sql2 = "update contacts set con_name=?,con_tel=?,con_fax=?,con_email=?,con_duties=? where con_agent_id=?";
            Object[] par2 = {contacts.getCon_name(), contacts.getCon_tel(), contacts.getCon_fax(), contacts.getCon_email(), contacts.getCon_duties(), customer.getCus_id()};
            i1 = super.executeUpdate(sql2, par2);
        }else{
            System.out.println(contacts.getCon_name());
            if(!contacts.getCon_name().equals("0")) {
                String sql2 = "insert into contacts(con_name,con_tel,con_fax,con_email,con_duties,con_agent_id) values(?,?,?,?,?,?)";
                Object[] par2 = {contacts.getCon_name(), contacts.getCon_tel(), contacts.getCon_fax(), contacts.getCon_email(), contacts.getCon_duties(), customer.getCus_id()};
                i1 = super.executeUpdate(sql2, par2);
            }
        }
//        if(contacts.getCon_id()!=0) {
//            String sql2 = "update contacts set con_name=?,con_tel=?,con_fax=?,con_email=?,con_duties=? where con_agent_id=?";
//            Object[] par2 = {contacts.getCon_name(), contacts.getCon_tel(), contacts.getCon_fax(), contacts.getCon_email(), contacts.getCon_duties(), customer.getCus_id()};
//             i1 = super.executeUpdate(sql2, par2);
//        }else{
//            String sql2="insert into contacts(con_name,con_tel,con_fax,con_email,con_duties,con_agent_id) values(?,?,?,?,?,?)";
//            Object[] par2={contacts.getCon_name(),contacts.getCon_tel(), contacts.getCon_fax(), contacts.getCon_email(), contacts.getCon_duties(),customer.getCus_id()};
//             i1 = super.executeUpdate(sql2, par2);
//        }
       if (i0>0&&i1>0){
        return i0;
       }else {
           return -1;
       }
    }

    @Override
    public int addKeyWords(Keyword keyword) throws SQLException {
        //判断关键词是否存在
        String sql1="select kw_name from keyword where kw_name=?";
        Object[] par1={keyword.getKw_name()};
        int i1 = super.findAllForInt(sql1, par1);
        if(i1==0){
            String sql="insert into keyword(kw_name,customer_name,service_type,application_period,application_begin,application_end,kw_price,kw_agent_id) values(?,?,?,?,?,?,?,(select cus_id from agent_customer where company_name=?))";
            Object[] par={keyword.getKw_name(),keyword.getCustomer_name(),keyword.getService_type(),keyword.getApplication_period(),keyword.getApplication_begin(),keyword.getApplication_end(),keyword.getKw_price(),keyword.getCustomer_name()};
            int i = super.executeUpdate(sql, par);
            System.out.println(i);
            return i;
        }else {
            return -1;
        }



    }

    @Override
    public int keyWordCustomer(String customerName) throws SQLException {
        String sql="select * from agent_customer where company_name=?";
        Object[] par={customerName};
        return super.findAllForInt(sql,par);
    }
}
